package com.wordpress.music2dot0.server;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GroupServer {
	private final List<Connection> members = new ArrayList<Connection>();
	private final Playlist playlist = Playlist.create(1, "metal");

	public void addConnection(Connection conn) {
		this.members.add(conn);
	}

	public void playNext() {
		this.playlist.playNext(this);
		for (final Connection member : this.members) {
			try {
				member.send(this.playlist.marshal());
			} catch (final IOException e) {
				LoginServer.log.throwing("GroupServer", "playNext", e);
				LoginServer.log.info("Removing member");
				this.members.remove(member);
			}
		}
	}

	public void vote(String genre) {
		LoginServer.log.info("Received vote for " + genre);
		this.playlist.addGenre(1, genre);
	}
}
